package duylinh

import org.springframework.dao.DataIntegrityViolationException

class CongNgheController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [congNgheInstanceList: CongNghe.list(params), congNgheInstanceTotal: CongNghe.count()]
    }



    def createEditCongNgheFlow = {
         start{
             action{
               def congNghe
               if ( params.congNgheId )
               {
                   congNghe = CongNghe.findById( params.congNgheId )
                   flow.isNewCongNghe = false
               }
               else
               {
                   congNghe = new CongNghe()
                   flow.isNewCongNghe = true
               }
               flow.congNghe = congNghe
             }
             on("success").to("basicInfo")
         }

         basicInfo{
            on("tiep"){
                flow.congNghe.properties = params

                def user = User.get(springSecurityService.principal.id)
                flow.creator = user

                if( user )
                {
                    if ( flow.isNewCongNghe )
                    {
                        flow.congNghe?.creator = user
                    }
                    else
                    {
                        flow.congNghe?.updater = user
                    }
                }
                println("user: " + user)


                if( !flow.congNghe.validate() )
                {
                    return error()
                }
                else
                {
                   flow.congNghe.save(flush:true)
                }

            }.to ("themOrSuaNguyenCong")
            on("huy").to("thoatFlow")
         }

         themOrSuaNguyenCong{
             action{
                 def nguyenCong

                 if( params.nguyenCongId )
                 {
                     nguyenCong = NguyenCong.findById( params.nguyenCongId )
                     flow.themNguyenCongMoi = false
                 }
                 else
                 {
                     nguyenCong = new NguyenCong( )
                     flow.themNguyenCongMoi = true
                 }
                 flow.nguyenCong = nguyenCong
                 println("flow.nguyenCong... " + flow.nguyenCong)
             }
             on("success").to("themNguyenCong")
         }

         themNguyenCong{
            on( "save" ){
                def nc = flow.nguyenCong
                nc.properties = params
                nc.creator = flow.creator

                if( !nc.validate() )
                {
                    return error()
                }
                else
                {
                    if ( flow.themNguyenCongMoi )
                    {
                        flow.congNghe?.addToNguyenCongs( nc )
                        nc.save(flush: true)
                        flow.congNghe.save(flush: true)
                    }
                    else
                    {
                        nc.save(flush: true)
                    }

                    println("save flush after...")
                }
            }.to("themOrSuaNguyenCong")
             on("suaNguyenCong").to ("themOrSuaNguyenCong")
             on("xoaNguyenCong").to ("xoaNguyenCong")
             on("upThuTuNguyenCong").to ("upThuTuNguyenCong")
             on("tiep").to("thoatFlow")
             on( "huy" ).to("thoatFlow")
         }

         xoaNguyenCong{
             action{
                 println("params: " + params)
                 if( params.nguyenCongId )
                 {
                     def nc = NguyenCong.findById( params.nguyenCongId )
                     if( nc )
                     {
                         flow.congNghe?.removeFromNguyenCongs( nc )
                         nc.delete()
                         params.nguyenCongId = null
                     }
                 }
             }
             on("success").to ("themOrSuaNguyenCong")
         }

         thoatFlow{
            redirect(action: "list")
         }
    }



    def create() {
        [congNgheInstance: new CongNghe(params)]
    }

    def save() {
        def congNgheInstance = new CongNghe(params)
        if (!congNgheInstance.save(flush: true)) {
            render(view: "create", model: [congNgheInstance: congNgheInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'congNghe.label', default: 'CongNghe'), congNgheInstance.id])
        redirect(action: "show", id: congNgheInstance.id)
    }

    def show(Long id) {
        def congNgheInstance = CongNghe.get(id)
        if (!congNgheInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'congNghe.label', default: 'CongNghe'), id])
            redirect(action: "list")
            return
        }

        [congNgheInstance: congNgheInstance]
    }

    def edit(Long id) {
        def congNgheInstance = CongNghe.get(id)
        if (!congNgheInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'congNghe.label', default: 'CongNghe'), id])
            redirect(action: "list")
            return
        }

        [congNgheInstance: congNgheInstance]
    }

    def update(Long id, Long version) {
        def congNgheInstance = CongNghe.get(id)
        if (!congNgheInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'congNghe.label', default: 'CongNghe'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (congNgheInstance.version > version) {
                congNgheInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'congNghe.label', default: 'CongNghe')] as Object[],
                          "Another user has updated this CongNghe while you were editing")
                render(view: "edit", model: [congNgheInstance: congNgheInstance])
                return
            }
        }

        congNgheInstance.properties = params

        if (!congNgheInstance.save(flush: true)) {
            render(view: "edit", model: [congNgheInstance: congNgheInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'congNghe.label', default: 'CongNghe'), congNgheInstance.id])
        redirect(action: "show", id: congNgheInstance.id)
    }

    def delete(Long id) {
        def congNgheInstance = CongNghe.get(id)
        if (!congNgheInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'congNghe.label', default: 'CongNghe'), id])
            redirect(action: "list")
            return
        }

        try {
            congNgheInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'congNghe.label', default: 'CongNghe'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'congNghe.label', default: 'CongNghe'), id])
            redirect(action: "show", id: id)
        }
    }
}
